using System;
using System.Collections;
using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

namespace LocalBoard.Persistence.SqlAccessor
{
	/// <summary>
	/// Summary description for SqlClubCategoryAccessor.
	/// </summary>
	public class SqlClubCategoryAccessor : SqlDataAccessBase, IClubCategoryAccessor
	{
		public static readonly SqlClubCategoryAccessor Instance = new SqlClubCategoryAccessor();

		private SqlClubCategoryAccessor()
		{}

		/// <summary>
		/// Delete club category
		/// </summary>
		/// <param name="id">Club category id</param>
		public void Delete(int id)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, "DeleteClubCategory", id);
		}

		/// <summary>
		/// Get all club categories
		/// </summary>
		/// <returns>Hashtable of all categories with category id as key and category name as value</returns>
		public Hashtable GetAllClubCategories()
		{
			using (SqlDataReader dataReader = SqlHelper.ExecuteReader(ConnectionString, "GetAllClubCategories"))
			{
				Hashtable clubCategories = new Hashtable();
				while(dataReader.Read())
				{
					clubCategories.Add(DataConverter.GetInteger(dataReader["Id"]), DataConverter.GetString(dataReader["CategoryName"]));
				}
				return clubCategories;
			}
		}

		/// <summary>
		/// Insert club category
		/// </summary>
		/// <param name="clubCategoryName">Club category name</param>
		/// <returns>Club category id</returns>
		public int Insert(string clubCategoryName)
		{
			if(clubCategoryName == null || clubCategoryName.Length == 0)
				throw new ArgumentException("Club Category shouldn't be null or empty string", "clubCategoryName");
			return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, "InsertClubCategory", clubCategoryName));
		}

		/// <summary>
		/// Update club category
		/// </summary>
		/// <param name="clubCategoryName">New club category</param>
		/// <param name="id">Club category id to change</param>
		public void Update(string clubCategoryName, int id)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, "UpdateClubCategory", clubCategoryName, id);
		}
	}
}
